<template>
  <vxe-modal
    v-model="visible"
    ref="mymodal"
    showFooter
    esc-closable
    title="修改快递100账号"
    width="540"
    @close="closeFrom"
    @hide="closeFrom"
    remember
  >
    <div class="form-contianer">
      <el-form ref="newForm" :model="form" :rules="rules" label-width="105px">
        <el-form-item label="名称" prop="name">
          <el-input
            v-model="form.name"
            name="name"
            placeholder="快递账号名称，用以快速识别"
          ></el-input>
        </el-form-item>
        <el-form-item label="快递100账号" prop="account">
          <el-input
            v-model="form.account"
            name="account"
            placeholder="快递100账号"
          ></el-input>
        </el-form-item>
        <el-form-item label="Key" prop="key">
          <el-input
            v-model="form.key"
            name="key"
            placeholder="快递100账号key"
          ></el-input>
        </el-form-item>
        <el-form-item label="Secret" prop="secret">
          <el-input
            v-model="form.secret"
            name="secret"
            placeholder="快递100账号secret"
          ></el-input>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-switch
            name="status"
            v-model="form.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="active_value"
            :inactive-value="inactive_value"
          >
          </el-switch>
        </el-form-item>
      </el-form>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button @click="closeFrom" size="small">取 消</el-button>
      <el-button
        type="primary"
        @click.native.prevent="submitHandle"
        :loading="loading"
        size="small"
        >修 改</el-button
      >
    </div>
  </vxe-modal>
</template>

<script>
import { edit } from '@/api/express'
export default {
  name: 'EditPriority',
  props: {
    editDialog: {
      type: Boolean
    },
    info: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  watch: {
    editDialog(val) {
      this.visible = val
    },
    info: {
      deep: true,
      handler(newVal, oldVal) {
        this.form = {
          id: newVal.info.id || null,
          name: newVal.info.name || '',
          account: newVal.info.account || '',
          key: newVal.info.key || '',
          secret: newVal.info.secret || '',
          status: newVal.info.status || 0
        }
      }
    }
  },
  created() {},
  data() {
    return {
      visible: false,
      loading: false,
      active_value: 1,
      inactive_value: 0,
      form: {
        id: '',
        name: '',
        account: '',
        key: '',
        secret: '',
        status: 0
      },

      rules: {
        name: [
          { required: true, message: '请输入快递账号名称', trigger: 'blur' }
        ],
        account: [
          { required: true, message: '请输入快递100账号', trigger: 'blur' }
        ],
        key: [
          { required: true, message: '请输入快递100账号key', trigger: 'blur' }
        ],
        secret: [
          {
            required: true,
            message: '请输入快递100账号secret',
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    closeFrom() {
      this.$emit('update:editDialog', false)
    },
    submitHandle() {
      const that = this
      this.$refs.newForm.validate((valid) => {
        if (valid) {
          this.loading = true
          edit(that.form)
            .then((res) => {
              this.$emit('updateList', this.form)
              this.loading = false
              this.$message({
                message: res.msg,
                type: 'success'
              })
              setTimeout(() => {
                that.closeFrom()
              }, 1000)
            })
            .catch((res) => {
              this.loading = false
            })
        } else {
          this.loading = false
          return false
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.form-contianer {
  padding: 25px 30px;
}
</style>
